package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private HttpServletRequest request;


    @Override
    public Result submit(SubmitDto submitDto) {
//        //将数据分别存在t_member和t_order
//        //todo 判断手机号是否为登录手机号
//        HttpSession session = request.getSession();
//        String telephone = (String) session.getAttribute("phone");
//        if (!telephone.equals(submitDto.getTelephone())) {
//            return new Result(false, "与登录手机号不符");
//        }
        //todo 判断姓名与表中是否一致
        LambdaUpdateWrapper<Member> memberWrapper = new LambdaUpdateWrapper<>();
        memberWrapper.eq(Member::getPhoneNumber, submitDto.getTelephone());
        Member mem = memberMapper.selectOne(memberWrapper);
        if (submitDto.getName() == null) {
            return new Result(false, "体检人不能为空");
        }
        if ( mem.getName() != null && !mem.getName().equals(submitDto.getName())) {
            return new Result(false, "不能给他人预约体检");
        }
        //todo 检查预约日期
        if (submitDto.getOrderDate() == null) {
            return new Result(false, "预约日期不能为空");
        }
        //存入t_member
        Member member = new Member();
        member.setName(submitDto.getName());
        member.setIdCard(submitDto.getIdCard());
        member.setSex(submitDto.getSex());
        LambdaUpdateWrapper<Member> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Member::getPhoneNumber, submitDto.getTelephone());
        memberMapper.update(member, wrapper);
        //存入t_order
        LambdaUpdateWrapper<Member> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.eq(Member::getPhoneNumber, submitDto.getTelephone());
        Member member1 = memberMapper.selectOne(wrapper1);

        Order order = new Order();
        order.setMemberId(member1.getId());
        order.setOrderDate(submitDto.getOrderDate());
        order.setSetmealId(submitDto.getSetmealId());
        order.setOrderType("微信预约");
        order.setOrderStatus("未到诊");
        orderMapper.insert(order);
        //返回order_id
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(submitDto.getOrderDate());
        LambdaQueryWrapper<Order> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Order::getMemberId, order.getMemberId());
        wrapper2.eq(Order::getSetmealId, submitDto.getSetmealId());
        wrapper2.eq(Order::getOrderDate, format);
        Order order1 = orderMapper.selectOne(wrapper2);
        return new Result(true, "success", order1.getId());
    }

    @Cacheable(value = "setmeal",key = "#root.args[0]",unless = "#result == null ")
    @Override
    public Result findById(Integer id) {
        //查找各个表中的信息
        Order order = orderMapper.selectById(id);
        Setmeal setmeal = setmealMapper.selectById(order.getSetmealId());
        Member member = memberMapper.selectById(order.getMemberId());
        //封装数据
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setMember(member.getName());
        orderInfo.setSetmeal(setmeal.getName());
        orderInfo.setOrderDate(order.getOrderDate());
        orderInfo.setOrderType(order.getOrderType());
        return new Result(true, "success", orderInfo);
    }
}
